package com.ruoyi.energy.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.energy.domain.vo.ParkCarResponseVO;
import com.ruoyi.energy.utils.ConstantValue;
import com.ruoyi.energy.utils.ParkCarUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.energy.mapper.TcParkMapper;
import com.ruoyi.energy.domain.TcPark;
import com.ruoyi.energy.service.ITcParkService;



/**
 * 停车场Service业务层处理
 *
 * @author cjie
 * @date 2021-11-30
 */
@Service
public class TcParkServiceImpl implements ITcParkService {
    @Autowired
    private TcParkMapper tcParkMapper;

    /**
     * 查询停车场
     *
     * @param id 停车场ID
     * @return 停车场
     */
    @Override
    public TcPark selectTcParkById(Long id) {
        return tcParkMapper.selectTcParkById(id);
    }

    @Override
    public TcPark selectTcParkByParkId(String id) {
        TcPark query = new TcPark();
        query.setParkId(id);

        List<TcPark> result = tcParkMapper.selectTcParkList(query);
        if (CollectionUtil.isEmpty(result)) {
            return null;
        }
        return result.get(0);

    }

    /**
     * 查询停车场列表
     *
     * @param tcPark 停车场
     * @return 停车场
     */
    @Override
    public List<TcPark> selectTcParkList(TcPark tcPark) {
        return tcParkMapper.selectTcParkList(tcPark);
    }

    /**
     * 新增停车场
     *
     * @param tcPark 停车场
     * @return 结果
     */
    @Override
    public int insertTcPark(TcPark tcPark) {
        //tcPark.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        tcPark.setCreateTime(DateUtils.getNowDate());
        tcPark.setCreateBy(SecurityUtils.getUsername());
        return tcParkMapper.insertTcPark(tcPark);
    }

    /**
     * 修改停车场
     *
     * @param tcPark 停车场
     * @return 结果
     */
    @Override
    public int updateTcPark(TcPark tcPark) {
        return tcParkMapper.updateTcPark(tcPark);
    }

    /**
     * 批量删除停车场
     *
     * @param ids 需要删除的停车场ID
     * @return 结果
     */
    @Override
    public int deleteTcParkByIds(Long[] ids) {
        return tcParkMapper.deleteTcParkByIds(ids);
    }

    /**
     * 删除停车场信息
     *
     * @param id 停车场ID
     * @return 结果
     */
    @Override
    public int deleteTcParkById(Long id) {
        return tcParkMapper.deleteTcParkById(id);
    }

    @Override
    public AjaxResult setPush(Long id) {
        TcPark park = tcParkMapper.selectTcParkById(id);
        if (park == null || StringUtils.isEmpty(park.getParkId())){
            return AjaxResult.error("ParkId不能为空");
        }
        try {
            ParkCarResponseVO response = ParkCarUtil.setServiceAddress(park.getParkId(), ConstantValue.PUSH_URL_PROD);
            if (response != null && response.getRetCode().equals("1")){
                return AjaxResult.success();
            }else{
                return AjaxResult.error(response.getErrorDes());
            }
        }catch (Exception e){
            AjaxResult.error("系统异常：" + e.getMessage());
        }
        return AjaxResult.error("系统异常");
    }
}
